package com.david.func_test

import android.app.Dialog
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView


class TestDialog : Dialog {

    private lateinit var listRecycler: RecyclerView
    private lateinit var gridRecycler: RecyclerView

    constructor(context: Context) : super(context, R.style.FullScreenDialogStyle)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_test)
//        val window = getWindow();
//        val params = window?.getAttributes();
//        params?.width = WindowManager.LayoutParams.MATCH_PARENT;
//        params?.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        window?.setAttributes(params);
//        window?.setGravity(Gravity.BOTTOM);

        setCanceledOnTouchOutside(true);
        initList()
        initGrid()

    }

    private fun initList() {
        listRecycler = findViewById(R.id.list_recycler)
        listRecycler.layoutManager = LinearLayoutManager(context)
        listRecycler.adapter = ListAdapter()
    }

    private fun initGrid() {
        gridRecycler = findViewById(R.id.grid_recycler)
        val gridLayoutManager = GridLayoutManager(context, 3)

        gridLayoutManager.spanSizeLookup = object :GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return if (position > 0) {
                    1//其他item只占一行中的一列，
                } else {
                    gridLayoutManager.spanCount//第个item时独占一行
                }
            }
        }

//        gridRecycler.layoutManager = gridLayoutManager
//        gridRecycler.adapter = GridAdapter()
    }

    var height: Int = 0


    fun showViewBottom(view: View) {
        if (height == 0) {
            val location = IntArray(2)
            view.getLocationInWindow(location)
            location[0] // view距离window 左边的距离（即x轴方向）

            val y = location[1] // view距离window 顶边的距离（即y轴方向）


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                height = window?.windowManager?.currentWindowMetrics?.bounds?.height()!!
            } else {
                val outMetrics = DisplayMetrics()
                val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
                wm.defaultDisplay.getRealMetrics(outMetrics)
                height = outMetrics.heightPixels
            }


            val params = window?.attributes
            params?.width = WindowManager.LayoutParams.MATCH_PARENT
            params?.height = (height - y - view.height)
            window?.attributes = params
            window?.setGravity(Gravity.BOTTOM)
        }

        show()
    }
}

class ListAdapter : RecyclerView.Adapter<ListHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
        val view = TextView(parent.context)
        view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100)
        return ListHolder(view)
    }

    override fun onBindViewHolder(holder: ListHolder, position: Int) {
    }

    override fun getItemCount(): Int {
        return 0
    }

}

class ListHolder(item: TextView) : RecyclerView.ViewHolder(item) {
    var title: TextView = item
}

//class GridAdapter : RecyclerView.Adapter<GridHolder>() {
//    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GridHolder {
//
//    }
//
//    override fun onBindViewHolder(holder: GridHolder, position: Int) {
//    }
//
//    override fun getItemCount(): Int {
//        return 0
//    }
//
//}

class GridHolder(item: View) : RecyclerView.ViewHolder(item) {

}